<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="gtitle" style="margin-bottom: 20px"><a-icon type="info-circle" style="color: #108ee9" theme="filled"/>系统运行状态监视</div>

      <a-spin :spinning="spinning">
      <div class="rowtxt">
        <a-row class='rowmb'>
          <a-col :span="2">CPU使用率:</a-col>
          <a-col :span="4" class="blue">{{cpuused}}</a-col>
          <a-col :span="2" :offset="16"><a-button type="primary" @click="getcpuinfo()"><a-icon type="sync" />刷新</a-button></a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2">内存总大小:</a-col>
          <a-col :span="4" class="blue">{{totalmemory}}</a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2">内存使用率:</a-col>
          <a-col :span="4" class="blue">{{memoryused}}</a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2">磁盘总空间:</a-col>
          <a-col :span="4" class="blue"></a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2">磁盘剩余空间:</a-col>
          <a-col :span="4" class="blue">{{romremain}}</a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2">磁盘使用率:</a-col>
          <a-col :span="4" class="blue">{{romused}}</a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2">磁盘使用率:</a-col>
          <a-col :span="4" class="blue"></a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2">网络IO（IN）:</a-col>
          <a-col :span="4" class="blue">{{speedin}}</a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2">网络IO（OUT）:</a-col>
          <a-col :span="4" class="blue">{{speedon}}</a-col>
        </a-row>
      </div>
      </a-spin>

      <div class="gtitle" style="margin-bottom: 20px;margin-top: 20px"><a-icon type="info-circle" style="color: #108ee9" theme="filled"/>系统运行状态监视</div>
      <div class="rowtxt">
        <a-row class='rowmb'>
          <a-col :span="2">系统当前时间:</a-col>
          <a-col :span="4" class="blue">{{moment(systermtime).format('YYYY-MM-DD HH:mm:ss')}}</a-col>
          <a-col :span="2" >
            <div style="overflow: hidden;position: relative;">
              <a-button type="primary" @click="modifysystermtime()" style="width: 100%">修改</a-button>
              <a-date-picker
                format="YYYY-MM-DD HH:mm:ss"
                ref="timeselect"
                style="opacity: 1;cursor: pointer;position: absolute;left: 0;top:0;opacity: 0"
                v-model="selectedtime"
                @change="updatesystermtime()"
              />
            </div>
          </a-col>
        </a-row>

        <a-row class='rowmb'>
          <a-col :span="2">是否开启自动同步:</a-col>
          <a-col :span="4"><a-switch checked-children="是" un-checked-children="否" v-model="config.isOpenSyncTime" @change="updateconfig('isOpenSyncTime',config.isOpenSyncTime)"/></a-col>
        </a-row>

        <a-row class='rowmb'>
          <a-col :span="2">NTP服务器:</a-col>
          <a-col :span="4"><a-input placeholder="ntp.ntsc.ac.cn" v-model="config.syncTimeNtpServerUrl" @blur="updateconfig('syncTimeNtpServerUrl',config.syncTimeNtpServerUrl)"></a-input></a-col>
        </a-row>
      </div>

      <div class="gtitle" style="margin-bottom: 20px;margin-top: 20px"><a-icon type="info-circle" style="color: #108ee9" theme="filled"/>系统服务配置</div>
      <div class="rowtxt">
        <a-row class='rowmb'>
          <a-col :span="2">SSH服务:</a-col>
          <a-col :span="4"><a-switch checked-children="开" un-checked-children="关" v-model="config.sshService" @change="updateconfig('sshService',config.sshService)" /></a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2">ICMP服务:</a-col>
          <a-col :span="4"><a-switch checked-children="开" un-checked-children="关" v-model="config.icmpService" @change="updateconfig('icmpService',config.icmpService)" /></a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2">防火墙服务:</a-col>
          <a-col :span="4"><a-switch checked-children="开" un-checked-children="关" v-model="config.firewallService" @change="updateconfig('firewallService',config.firewallService)" /></a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2">禁止访问地址:</a-col>
          <a-col :span="4"><a-input placeholder="请输入IP地址，IP段或IP掩码" v-model="ipinput"></a-input></a-col>
          <a-col :span="2" :offset="1"><a-button type="primary" @click="addip()">添加</a-button></a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="4" :offset="2">
            <div class="iplist">
              <div v-for="(item2,index) in ipjson.ip">
                {{item2.value}}
                <a-icon type="delete" class="icon" @click="deleteip(index)"/>
              </div>
            </div>
          </a-col>
        </a-row>
      </div>

      <div class="gtitle" style="margin-bottom: 20px;margin-top: 20px"><a-icon type="info-circle" style="color: #108ee9" theme="filled"/>系统运行管理</div>
      <div class="rowtxt">
        <a-row class='rowmb'>
          <a-col :span="2">WEB应用服务:</a-col>
          <a-col :span="4"><a-button type="primary">重启</a-button></a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2">系统主服务:</a-col>
          <a-col :span="4"><a-button type="primary">重启</a-button></a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2">网卡服务:</a-col>
          <a-col :span="4"><a-button type="primary">重启</a-button></a-col>
        </a-row>
        <a-row class='rowmb'>
          <a-col :span="2">平台主机服务:</a-col>
          <a-col :span="2"><a-button type="primary">重启</a-button></a-col>
          <a-col :span="2"><a-button type="primary">关机</a-button></a-col>
        </a-row>
      </div>
    </a-card>
  </page-header-wrapper>
</template>

<script>
  import {} from '../../utils/constant'
  import { getServerTime,getcommoncomfig,updateServerTime,updatecommonconfig,getsysterm } from '@/api/systerm'
  import moment from 'moment';
  export default {
    name: 'PatchUpdateManage',
    data() {
      return {
        cpuused:'',
        totalmemory:'',
        memoryused:'',
        romremain:'',
        romused:'',
        speedin:'',
        speedon:'',
        systermtime:'',
        ipinput:'',
        ipjson:{
          ip:[]
        },
        selectedtime:'',
        config:{},
        spinning:false
      }
    },
    mounted() {

      this.$info(
        {
          title:'提示',
          content:'测试Lisence未授权！',
          onOk:()=> {
            this.$router.push({ path: '/overview/overview' })
          },
        }
      )

      this.getServerTime()
      this.getcommonconfig()

      this.getcpuinfo()

    },
    methods:{
      moment,
      getcpuinfo(){
        this.spinning = true
        fetch( process.env.VUE_APP_FETCH+'/web/system_configure',{method:'get'}).then(res=>{
          return res.json()
        }).then(res=>{
          this.spinning = false
          if(res.data.cpu_used)this.cpuused = res.data.cpu_used + '%'
          if(res.data.memory_used)this.memoryused = res.data.memory_used + '%'
          if(res.data.rom_remaind)this.romremain = res.data.rom_remaind +'GB'
          if(res.data.rom_used) this.romused = res.data.rom_used + '%'
          if(res.data.tx_in_speed) this.speedin = res.data.tx_in_speed + ' kbps'
          if(res.data.tx_out_speed) this.speedon = res.data.tx_out_speed + ' kbps'
        })
      },
      getcommonconfig(){
        getcommoncomfig({id:1}).then(res=>{
          res.data.isOpenSyncTime = res.data.isOpenSyncTime==1?true:false
          res.data.sshService = res.data.sshService==1?true:false
          res.data.icmpService = res.data.icmpService==1?true:false
          res.data.firewallService = res.data.firewallService==1?true:false
          if(res.data.accessDeniedIps ){
            this.ipjson = JSON.parse(res.data.accessDeniedIps)
          }
          this.config = res.data
        })
      },
      updateconfig(key,value){
        let json = {}
        json.id = 1
        if(key == 'isOpenSyncTime'){
          value= value?1:0
        }
        if(key == 'sshService'){
          value= value?1:0
        }
        if(key == 'icmpService'){
          value= value?1:0
        }
        if(key == 'firewallService'){
          value= value?1:0
        }

        json[key] = value

        updatecommonconfig(json).then(res=>{})
      },
      updatesystermtime(){
        updateServerTime({newSystemTime:this.selectedtime}).then(res=>{

        })
      },
      getServerTime(){
        getServerTime({}).then(res=>{
          this.systermtime = new Date(res.data).getTime()
          setInterval(()=>{
            this.systermtime += 1000
          },1000)
        })
      },
      addip(){
        if(this.ipinput){
          let ipreg = /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/
          let ipdreg = /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)(-)((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/
          let ipyreg = /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)(\/)(\d|[1-2]\d|3[0-2])$/
          let ip = this.ipinput
          if(!ipreg.test(ip) && !ipdreg.test(ip) && !ipyreg.test(ip)){
            this.$message.error('格式不正确')
            return
          }

          let json = {}
          if(this.ipinput.indexOf('-')>-1){
            json.type = 3
          }else if(this.ipinput.indexOf('/')>-1){
            json.type = 2
          }else{
            json.type = 1
          }
          json.value = this.ipinput
          this.ipjson.ip.push(json)
          this.ipinput = ''

          let params = {}
          params.id = 1
          params.accessDeniedIps = JSON.stringify(this.ipjson)
          updatecommonconfig(params).then(res=>{

          })


        }
      },
      deleteip(index){
        this.ipjson.ip.splice(index,1)
        let params = {}
        params.id = 1
        params.accessDeniedIps = JSON.stringify(this.ipjson)
        updatecommonconfig(params).then(res=>{

        })
      },

    }
  }
</script>

<style lang="less"  scoped>

  .gtitle{
    background-color: #e6f3fc;
    line-height: 40px;
    padding-left: 20px;
    color: #333;
    font-weight: bold;

    i{
      margin-right: 10px;
    }
  }
  .rowtxt,.ant-radio-group{
    line-height: 32px;

  }
  .ant-radio-group{
    width: 100%;
  }
  .rowspan{
    display: inline-block;
    width: 150px;
  }
  .showtxt{
    border: 1px solid #ddd;
    border-radius: 4px;
    padding: 0 10px;
    height: 120px;
    overflow-y: auto;
  }
  .rowmb{
    margin-bottom: 10px;
  }
  .blue{
    color: #3366FF;
  }
  .iplist{
    border: 1px solid #ddd;
    padding: 5px;
    min-height: 100px;
    line-height: 30px;
    div:hover{
      background-color: #efefef;
    }
    .icon{
      color: red;
      cursor: pointer;
      line-height: 30px;
      float: right;
      margin-right: 20px;
    }
  }
</style>
